package com.phkj.qdh.ui.fragment.teaminspect

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.bean.BaseInspectBean
import com.phkj.qdh.bean.DeptTreeVoList
import com.phkj.qdh.bean.Detail
import com.phkj.qdh.bean.KeyPointData
import com.phkj.qdh.bean.KeyPointDataBean
import com.phkj.qdh.bean.LawOrTeamInspectBean
import com.phkj.qdh.bean.TeamScoreBean
import com.phkj.qdh.net.code
import com.phkj.qdh.net.msg
import com.phkj.qdh.repository.CommonRepository
import com.phkj.qdh.repository.InspectRepository
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest

class TeamInspectVM(private val inspect: InspectRepository, val common: CommonRepository) :
    BaseVM() {

    private val _teamSortList = MutableSharedFlow<ArrayList<LawOrTeamInspectBean>>()
    val teamSortList get() = _teamSortList
    fun getTeamSort(parentId: Int) {
        requestIO {
            inspect.lawAndTeamTree(parentId).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _teamSortList.emit(it)
            }
        }
    }


    private val _scoreList = MutableSharedFlow<ArrayList<TeamScoreBean>>()
    val scoreList get() = _scoreList
    fun getScoreList(type:Int) {
        requestIO {
            inspect.getScoreList(type).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _scoreList.emit(it)
            }
        }
    }


    private val _saveLawInspectResult = MutableSharedFlow<Boolean>()
    val saveLawInspectResult get() = _saveLawInspectResult
    fun saveLawInspect(map: HashMap<String, Any?>) {
        requestIO {
            inspect.saveLawInspect(map).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _saveLawInspectResult.emit(it)
            }
        }
    }


    private val _saveTeamInspectResult = MutableSharedFlow<Boolean>()
    val saveTeamInspectResult get() = _saveTeamInspectResult
    fun saveTeamInspect(map: HashMap<String, Any?>) {
        requestIO {
            inspect.saveTeamInspect(map).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _saveTeamInspectResult.emit(it)
            }
        }
    }

    private val _uploadFileResult = MutableSharedFlow<String>()
    val uploadFileResult get() = _uploadFileResult

    fun uploadFile(filePath: String) {
        requestIO {
            common.uploadFile(filePath).catch { cause -> }.collectLatest {

            }
        }
    }


    private val _employee = MutableSharedFlow<ArrayList<DeptTreeVoList>>()
    val employee get() = _employee

    fun getEmployee() {
        requestIO {
            inspect.getEmployee().catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _employee.emit(it)
            }
        }
    }


    private val _employee1 = MutableSharedFlow<ArrayList<DeptTreeVoList>>()
    val employee1 get() = _employee1

    fun getEmployee1() {
        requestIO {
            inspect.getEmployee().catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _employee1.emit(it)
            }
        }
    }


    private val _baseInspectType = MutableSharedFlow<ArrayList<BaseInspectBean>>()
    val baseInspectType get() = _baseInspectType
    fun getBaseInspectType() {
        requestIO {
            inspect.getBaseInspectType().catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _baseInspectType.emit(it)
            }
        }
    }


    private val _baseInspectSave = MutableSharedFlow<Int>()
    val baseInspectSave get() = _baseInspectSave
    fun baseInspectSave(map: HashMap<String, Any?>) {
        requestIO {
            inspect.baseInspectSave(map).catch { cause ->
//                ToastUtils.show(cause.msg)
                baseInspectSave.emit(cause.code)
            }.collectLatest {
//                _baseInspectSave.emit(it)
            }
        }
    }

    private val _keyPointDataList = MutableSharedFlow<ArrayList<KeyPointData>>()
    val keyPointDataList = _keyPointDataList
    fun getKeypointDataList(map: HashMap<String, Any?>) {
        requestIO {
            inspect.baseKeyPointType(map).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _keyPointDataList.emit(it.data)
            }
        }
    }


    private val _baseScoreList = MutableSharedFlow<ArrayList<TeamScoreBean>>()
    val baseScoreList get() = _baseScoreList
    fun getBaseScoreList(type:Int) {
        requestIO {
            inspect.getBaseScoreList(type).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _baseScoreList.emit(it)
            }
        }
    }

}

fun getTeamInspectVMProvider() = TeamInspectVMProvider(InspectRepository, CommonRepository)

class TeamInspectVMProvider(
    private val inspect: InspectRepository, private val common: CommonRepository
) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return TeamInspectVM(inspect, common) as T
    }
}